import {_decorator, Component, Label, Sprite} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {ResourceUtils} from "db://assets/scripts/common/ResourceUtils";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {EffectManager} from "db://assets/scripts/common/EffectManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {PlayerSkillManager} from "db://assets/scripts/game/role/player/PlayerSkillManager";

const {ccclass, property} = _decorator;

/**
 * 技能控制器
 */
@ccclass('SkillItemController')
export class SkillItemController extends Component {
    /**
     * 图标
     */
    @property(Sprite)
    public iconSprite: Sprite;
    /**
     * 名称
     */
    @property(Label)
    public nameLabel: Label;
    /**
     * 描述
     */
    @property(Label)
    public descriptionLabel: Label;

    /**
     * 回调
     */
    private callback: Function;
    /**
     * 技能对象
     */
    private skill: Skill;

    /**
     * 初始化技能信息
     *
     * @param skill 技能信息
     * @param callback 回调
     */
    public init(skill: Skill, callback: Function) {
        this.skill = skill;
        this.callback = callback;

        this.nameLabel.string = skill.name;
        this.descriptionLabel.string = skill.description;

        ResourceUtils.setSpriteFrame(`texture/skillIcon/${skill.icon}`, this.iconSprite);
    }

    /**
     * 点击事件
     */
    public onClick() {
        // 声音
        AudioManager.getInstance().playSound(AudioPaths.getSkillSound);

        // 数据
        PlayerSkillManager.getInstance().addPlayerSkill(this.skill.id);

        // 特效
        EffectManager.getInstance().loadAndPlayEffect(true, GameManager.getInstance().gamePlayerProxy.getPlayerNode(), "levelUp/levelUp", 1, null, null, false, true, GameManager.getInstance().gameSpeedProxy.getGameSpeed(), true);

        // 回调
        this.callback && this.callback();
    }

}